package cn.poco.pepsi.adapter;

import java.util.List;

import cn.poco.pepsi.R;
import cn.poco.pepsi.bean.Topic;
import cn.poco.pepsi.util.AsyncImageLoader.ImageCallback;
import cn.poco.pepsi.util.AsyncLoadImageService;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Find_atc_Adapt extends BaseAdapter {

	private Context context;
	private List<Topic> list;
	private AsyncLoadImageService asyncImgLoader;
	private ListView listView;
	

	
	public Find_atc_Adapt(Context context, List<Topic> list,ListView listView) {
		super();
		this.context = context;
		this.list = list;
		this.listView=listView;
		this.asyncImgLoader = new AsyncLoadImageService(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final CustomView customView;
		
		Topic topic=list.get(position);
		if (convertView==null) {
			customView=new CustomView();
			convertView=LayoutInflater.from(context).inflate(R.layout.find_atc_item, null);
			customView.textView=(TextView)convertView.findViewById(R.id.atc_name);
			customView.textView2=(TextView)convertView.findViewById(R.id.atc_info);
			customView.img=(ImageView)convertView.findViewById(R.id.atc_image);
			convertView.setTag(customView);
		}else {
			customView=(CustomView)convertView.getTag();
		}
		customView.textView.setText(topic.getTitle());
		customView.textView.setTextColor(Color.BLACK);
		
		customView.textView2.setText(topic.getDiscription());
		customView.textView2.setTextColor(Color.BLACK);
		
		final String myUserIcon = topic.getImage();
		customView.img.setTag(myUserIcon);
		Bitmap drawUserIcon = asyncImgLoader.loadBitmap(myUserIcon, new ImageCallback() {
			@Override
			public void imageLoaded(Bitmap imageBitmap, String imageUrl) {
				ImageView imageViewByTag = (ImageView) listView.findViewWithTag(myUserIcon);
				if (imageViewByTag != null) {
					imageViewByTag.setImageBitmap(imageBitmap);
				}
			}
		}, true, false, 0);
		
		if(drawUserIcon==null){
			customView.img.setImageResource(R.drawable.load_guest);
		}else{
			customView.img.setImageBitmap(drawUserIcon);
		}
	
		
		
		return convertView;
	}
	
	class CustomView{
		TextView textView;
		TextView textView2;
		ImageView img;
		
		
	}


}
